<?php
class Cmsdoc extends TagsAbstract {
	/**
	 * 是否是封闭的标签
	 * @var unknown_type
	 */
	static $TAG_CLOSED = false;
	
	public $config = array();
	
	public function __construct(){

	}	
	public function getTagStatus(){
		return self::$TAG_CLOSED;
	}
	/**
	 * 返回模板文件路径
	 */
	public function getTemplateFile($tpl = '') {
		//返回需要渲染的模板
		$file = $this->attr ['style'];
	if(!empty($tpl)){
			$file = $tpl;
		}
		return dirname(__FILE__).'/cmsdoc/'.$file.'.html';
	}
	
	/**
	 * 这里返回的是模板中需要渲染的变量
	 */
	public function replace($checkCache) {
		$order  = isset($this->attr['order']) ? $this->attr['order'] : 'id DESC';
		$limit  = isset($this->attr['limit']) ? intval($this->attr['limit']) : 20;
		!isset($this->attr['words']) && $this->attr['words'] = 10;
		//未被删除
		$map['isDel'] = 0;
		
		//指定频道
		if(!empty($this->attr['channel']) && $this->attr['channel'] != 'ALL') {
			$channel = explode(',', $this->attr['channel']);
			$map['channelId'] = array('in', $channel);
		}

		//指定分类
		if(!empty($this->attr['cate']) && $this->attr['cate'] != 'ALL') {
			$cate = explode(',', $this->attr['cate']);
			$map['category'] = array('in', $cate);
		}

		//指定文章
		if(!empty($this->attr['ids'])) {
			$map['id'] = array('in', $this->attr['ids']);
		}

		//是否带图片
		if(!empty($this->attr['status']) && $this->attr['status'] == 1) {
			$map['img'] = array('exp', 'IS NOT NULL');
		}

		$res = M('portal_cms')->where($map)->order($order)->findPage($limit);

		$var['title'] = t($this->attr['title']);
		$var['list']  = $res['data'];
		$var['html']  = $res['html'];
		$var['attr']  = $this->attr;
		$var['image'] = $this->attr['image'];
		return $var;
	}


	/** ********************* 自定义函数 ********************* **/

	//将二维数组格式化为一维数组
	protected function _format($arr, $field) {
		$res = array();
		foreach($arr as $v) {
			$res[] = $v[$field];
		}
		return $res;
	}
}
?>